package com.dzzw.Dao;

import com.dzzw.Model.Reply;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//论坛评论
public class ReplyDao {
    private final Connection con = DButils.getCon();
    //新增评论
    public void ReplyAdd(Reply reply){
        String sql = "insert into t_reply(forum_id,user_id,response_content) value(?,?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1, reply.getForum_id());
            preparedStatement.setInt(2, reply.getUser_id());
            preparedStatement.setString(3, reply.getResponse_content());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //修改评论
    public void ReplyModify(Reply reply){
        String sql = "update t_reply set response_content=? where response_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, reply.getResponse_content());
            preparedStatement.setInt(2, reply.getResponse_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据修改失败！");

            e.printStackTrace();
        }
    }

    //删除评论
    public void ReplyDelete(int response_id){
        String sql = "Delete from t_reply where response_id = ?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1, response_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据修改失败！");

            e.printStackTrace();
        }
    }

    //根据用户id查询用户评论
    public static List<Reply> SelectAllReplyByUserID(int user_id){
        List<Reply> list = new ArrayList<>();
        String sql = "SELECT r.response_id,f.forum_title,u.user_name,r.response_content FROM t_reply AS r INNER JOIN t_user AS u ON r.user_id = u.user_id INNER JOIN t_forum AS f ON f.user_id = u.user_id AND r.forum_id = f.forum_id where r.user_id = ?";
        try{
            Connection con =DButils.getCon();
            PreparedStatement preparedStatement =con.prepareStatement(sql);
            preparedStatement.setInt(1,user_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Reply reply = new Reply();
                reply.setResponse_id(resultSet.getInt(1));
                reply.setForum_title(resultSet.getString(2));
                reply.setUser_name(resultSet.getString(3));
                reply.setResponse_content(resultSet.getString(4));
                list.add(reply);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //查询全部评论
    public static List<Reply> SelectAllReply(){

        List<Reply> list = new ArrayList<>();
        String sql = "SELECT r.response_id,f.forum_title,u.user_name,r.response_content FROM t_reply AS r INNER JOIN t_user AS u ON r.user_id = u.user_id INNER JOIN t_forum AS f ON f.user_id = u.user_id AND r.forum_id = f.forum_id";
        try{
            Connection con =DButils.getCon();
            PreparedStatement preparedStatement =con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Reply reply = new Reply();
                reply.setResponse_id(resultSet.getInt(1));
                reply.setForum_title(resultSet.getString(2));
                reply.setUser_name(resultSet.getString(3));
                reply.setResponse_content(resultSet.getString(4));
                list.add(reply);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
